package com.shenzw.common.sign;

import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Base64;

/**
 * @description: 加签controller
 * @author: shenzw
 * @date: 2024/10/25
 */
@RestController
@RequestMapping("/sign")
public class SignatureController {

    @Resource
    private VerificationController verificationController;

    @PostMapping
    @SignatureAnnotation
    public String signMessage(@RequestBody String request, byte[] signature) {
        // 此方法会自动加签
        System.out.println("原始报文: " + JSON.toJSONString(request));
        System.out.println("加签报文: " + Base64.getEncoder().encodeToString(signature));

        VerificationRequest verificationRequest = new VerificationRequest();
        verificationRequest.setData(request); // 加签前的数据, 但是经过了处理
        verificationRequest.setSignature(signature); // 加签后的数据
//        verificationRequest.setSignature(Base64.getEncoder().encodeToString(signature));

//        String s = verificationController.verifyMessage(message, signature);
        String s = verificationController.verifyMessage(verificationRequest); // 模拟调用被调用端

        return "Message signed successfully";
    }

    public static void main(String[] args) {
        System.out.println("123321".matches("^\\d+$"));
    }
}
